package com.elitel.frame.base.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elitel.frame.base.service.BaseMasterdetailService;
import com.elitel.frame.base.service.CfgDbService;
import com.elitel.frame.base.service.CfgExtmasterService;
import com.elitel.frame.base.service.CfgFieldService;
import com.elitel.frame.base.service.CfgTableService;
import com.elitel.frame.main.entity.BaseMasterdetail;
import com.elitel.frame.main.entity.CfgDb;
import com.elitel.frame.main.entity.CfgExtmaster;
import com.elitel.frame.main.entity.CfgField;
import com.elitel.frame.main.entity.CfgTable;
import com.elitel.frame.main.entity.ext.BaseMasterdetailExt;
import com.elitel.frame.main.entity.ext.CfgExtmasterExt;
import com.elitel.frame.main.entity.vo.KeyValueModelVo;
import com.elitel.frame.main.entity.vo.ZTreeDataModelVo;

/**
 * Description 主从表配置
 * Author guoyf
 */

@Controller
@RequestMapping("/system/masterDetail")
public class MasterDetailController {
	private static final Logger logger = LoggerFactory.getLogger(MasterDetailController.class);
	@Autowired
	private CfgExtmasterService cfgExtmasterService;
	@Autowired
	private CfgTableService cfgTableService;
	@Autowired
	private CfgFieldService cfgFieldService;
	@Autowired
	private BaseMasterdetailService baseMasterdetailService;
	@Autowired
	private CfgDbService cfgDbService;
	
	/**
	 * @Description 获取树形服务列表
	 * @return ZTreeDataModelVo集合
	 * @author guoyanfei
	 */
	@RequestMapping("/getmasterListtree")
	@ResponseBody
	public List<ZTreeDataModelVo> getmasterListtree(){
		List<ZTreeDataModelVo> ztreelist = new ArrayList<>();
		try {
			ztreelist = cfgExtmasterService.getmasterDetailTree();
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return ztreelist;
	}
	
	/**
	 * @description 查询所有数据源
	 * @return Cfgdb集合
	 * @author guoyanfei
	 * @date 2017/11/29
	 */
	@RequestMapping("/getAllDataSource")
	@ResponseBody
	public List<CfgDb> getAllDataSource(HttpServletRequest request){
		List<CfgDb> cfgDbs = new ArrayList<CfgDb>();
		try {
			cfgDbs = cfgDbService.getAllListCfgDb();
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return cfgDbs;
	}
	
	/**
	 * @Description 通过数据库标识查询所有表信息
	 * @param dbkey(数据库标识)
	 * @return cfgTable集合
	 * @author guoyanfei
	 */
	@RequestMapping("/getTabledatabydbkey")
	@ResponseBody
	public List<CfgTable> getTabledatabydbkey(HttpServletRequest request){
		List<CfgTable> cfgTables = new ArrayList<>();
		try {
			String dbkey = request.getParameter("dbkey");
			cfgTables = cfgTableService.getListbyConn(dbkey);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return cfgTables;
	}
	
	/**
	 * @description 查询服务ID是否存在
	 * @param guid(服务ID)
	 * @return true或false
	 * @author guoyanfei
	 * @date 2017/11/07
	 */
	@RequestMapping("/isExistGuid")
	@ResponseBody
	public boolean isExistGuid(HttpServletRequest request){
		boolean result = false;
		try {
			String guid = request.getParameter("guid");
			result = cfgExtmasterService.isExistGuid(guid);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * @description 插入主从表主表信息
	 * @param cfgExtmaster实体
	 * @return 插入条数
	 * @author guoyanfei
	 * @date 2017/11/7
	 */
	@RequestMapping("/insertMasterData")
	@ResponseBody
	public Integer insertMasterData(@RequestBody CfgExtmasterExt cfgExtmasterExt){
		Integer result = 0;
		try {
			result = cfgExtmasterService.insertMasterandSysprj(cfgExtmasterExt);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * @description 修改主表信息
	 * @param cfgExtmaster实体
	 * @return 修改条数
	 * @author guoyanfei
	 * @date 2017/11/07
	 */
	@RequestMapping("/updateMasterData")
	@ResponseBody
	public Integer updateMasterData(@RequestBody CfgExtmasterExt cfgExtmasterExt){
		Integer result = 0;
		try {
			CfgExtmaster cfgExtmaster = new CfgExtmaster();
			cfgExtmaster.setGuid(cfgExtmasterExt.getGuid());
			cfgExtmaster.setServicename(cfgExtmasterExt.getServicename());
			cfgExtmaster.setDbKey(cfgExtmasterExt.getDbKey());
			cfgExtmaster.setDtConfigId(cfgExtmasterExt.getDtConfigId());
			cfgExtmaster.setRemark(cfgExtmasterExt.getRemark());
			cfgExtmaster.setIspagination(cfgExtmasterExt.getIspagination());
			cfgExtmaster.setSyscatch(cfgExtmasterExt.getSyscatch());
			cfgExtmaster.setCatchtime(cfgExtmasterExt.getCatchtime());
			cfgExtmaster.setResponseMode(cfgExtmasterExt.getResponseMode());
			result = cfgExtmasterService.updateMasterdata(cfgExtmaster);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * @Description 删除主从表配置服务(包括从表数据)
	 * @param guid(主表服务ID)
	 * @return 删除结果
	 * @author guoyanfei
	 */
	@RequestMapping("/deleteMasterData")
	@ResponseBody
	public Integer deleteMasterData(HttpServletRequest request){
		Integer result = 0;
		try {
			String guid = request.getParameter("guid");
			result = cfgExtmasterService.deleteMasterdata(guid);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * @description 通过guid查询主表信息
	 * @param request
	 * @return
	 * @author guoyanfei
	 * @date 2017/11/07
	 */
	@RequestMapping("/getCfgExtmasterByguid")
	@ResponseBody
	public CfgExtmaster getCfgExtmasterByguid(HttpServletRequest request){
		CfgExtmaster cfgExtmaster = new CfgExtmaster();
		try {
			String guid = request.getParameter("guid");
			cfgExtmaster = cfgExtmasterService.getCfgExtmasterByguid(guid);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return cfgExtmaster;
		
	}
	
	/**
	 * @description 通过主表服务ID查询主表字段信息
	 * @param request
	 * @return
	 * @author guoyanfei
	 * @date 2017/11/07
	 */
	@RequestMapping("/getFieldbymasterguid")
	@ResponseBody
	public List<KeyValueModelVo> getFieldbymasterguid(HttpServletRequest request){
		List<KeyValueModelVo> cfgFields = new ArrayList<>();
		try {
			String guid = request.getParameter("guid");
			CfgExtmaster cfgExtmaster = cfgExtmasterService.getCfgExtmasterByguid(guid);
			if(cfgExtmaster == null)
				return cfgFields;

			cfgFields = cfgExtmasterService.selectFieldValue(cfgExtmaster.getDtConfigId());
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return cfgFields;
	}
	
	/**
	 * @description 通过从表dtconfigid查询从表字段信息
	 * @param request
	 * @return
	 * @author guoyanfei
	 * @date 2017/11/07
	 */
	@RequestMapping("/getFieldbydtconfigid")
	@ResponseBody
	public List<KeyValueModelVo> getFieldbydtconfigid(HttpServletRequest request){
		List<KeyValueModelVo> cfgFields = new ArrayList<>();
		try {
			String dtConfigid = request.getParameter("dtConfigid");
			cfgFields = cfgExtmasterService.selectFieldValue(dtConfigid);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return cfgFields;
	}
	
	/**
	 * @description 通过主表服务ID查询所在数据源所有表信息
	 * @param request
	 * @return
	 * @author guoyanfei
	 * @date 2017/11/07
	 */
	@RequestMapping("/getTablebyMasterguid")
	@ResponseBody
	public List<CfgTable> getTablebyMasterguid(HttpServletRequest request){
		List<CfgTable> cfgTables = new ArrayList<>();
		try {
			String guid = request.getParameter("guid");
			CfgExtmaster cfgExtmaster = cfgExtmasterService.getCfgExtmasterByguid(guid);
			if(cfgExtmaster == null)
				return cfgTables;

			cfgTables = cfgTableService.getListbyConn(cfgExtmaster.getDbKey());

		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return cfgTables;
	}
	
	/**
	 * @Description 新增从表信息
	 * @param baseMasterdetail实体
	 * @return 新增条数
	 * @author guoyanfei
	 */
	@RequestMapping("/insertMasterDetail")
	@ResponseBody
	public Integer insertMasterDetail(@RequestBody BaseMasterdetail baseMasterdetail){
		Integer result = 0;
		try {
			result = baseMasterdetailService.insertMasterDetail(baseMasterdetail);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * @Description 修改从表信息
	 * @param baseMasterdetail
	 * @return 修改条数
	 * @author guoyanfei
	 */
	@RequestMapping("/updateMasterDetail")
	@ResponseBody
	public Integer updateMasterDetail(@RequestBody BaseMasterdetail baseMasterdetail){
		Integer result = 0;
		try {
			result = baseMasterdetailService.updateMasterDetail(baseMasterdetail);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * @Description 通过guid删除从表信息
	 * @param request
	 * @return 删除条数
	 * @author guoyanfei
	 */
	@RequestMapping("/deleteMasterDetailbyguid")
	@ResponseBody
	public Integer deleteMasterDetailbyguid(HttpServletRequest request){
		Integer result = 0;
		try {
			String guid = request.getParameter("guid");
			result = baseMasterdetailService.deleteMasterDetail(guid);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * @Description 通过主表ID查询从表信息
	 * @param mguid(主表ID)
	 * @return BaseMasterdetail集合
	 * @author guoyanfei
	 */
	@RequestMapping("/getMasterDetailbymguid")
	@ResponseBody
	public List<BaseMasterdetail> getMasterDetailbymguid(HttpServletRequest request){
		List<BaseMasterdetail> baseMasterdetails = new ArrayList<>();
		try {
			String mguid = request.getParameter("mguid");
			baseMasterdetails = baseMasterdetailService.getMasterdetailByguid(mguid);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return baseMasterdetails;
	}
	
	/**
	 * @description 通过主表服务ID查询从表扩展信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/getExtMasterDetailBymguid")
	@ResponseBody
	public List<BaseMasterdetailExt> getExtMasterDetailBymguid(HttpServletRequest request){
		List<BaseMasterdetailExt> baseMasterdetailExts = new ArrayList<>();
		try {
			String mguid = request.getParameter("mguid");
			baseMasterdetailExts = baseMasterdetailService.getExtMasterdetailBymguid(mguid);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return baseMasterdetailExts;
	}
	
	
}
